﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Licensed under the MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////
#include "ActionUIFactory.h"

#include "IUiDocumentViewManager.h"
#include "GbmpNew.h"

#include "ActionBarDialog.h"
#include "ActionBarDialogReference.h"
#include "IApplicationWindow.h"
#include "ActionCoordinateInputWidget.h"
#include "CoordinateInputDefinition.h"
#include "IUiView.h"


#include "EnableCompileWarning_The_LAST_IncludeInCpp.h"
using namespace gcmp;

namespace
{
    ActionUIFactory actionUIFactory;
}

ActionUIFactory::ActionUIFactory()
{
    m_pInstance = this;
}

ActionUIFactory::~ActionUIFactory()
{
    m_pInstance = nullptr;
}

ActionUIFactory* ActionUIFactory::GetInstance()
{
    return &actionUIFactory;
}


OwnerPtr<IActionInputDialog> ActionUIFactory::CreateActionInputDialog(const std::wstring& title, std::vector<OwnerPtr<IControlDefinition>>&& controlDefinitions, IAction* pAction)
{
    IUiDocumentViewManager* pUiDocumentViewMgr = IUiDocumentViewManager::Get();
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pUiDocumentViewMgr, L"pUiDocumentViewMgr为空",L"GDMPLab",L"2024-03-30");

    gcmp::IUiView * pCurUIView = pUiDocumentViewMgr->GetCurrentUiView();
    if (!pCurUIView)
    {
        return nullptr;
    }

    QWidget* pViewWidget = QWidget::find((WId)pCurUIView->GetUiViewId());
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pViewWidget, L"pViewWidget为空",L"GDMPLab",L"2024-03-30");
    QWidget* pViewParent = pViewWidget->parentWidget();
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pViewParent, L"pViewParent为空",L"GDMPLab",L"2024-03-30");


    OwnerPtr<ActionBarDialogReference> opDialogReference(NEW_AS_OWNER_PTR(ActionBarDialogReference, pViewParent));
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(opDialogReference, L"opDialogReference初始化失败",L"GDMPLab",L"2024-03-30");
    opDialogReference->InitDlg(title, TransferOwnership(controlDefinitions), pAction);
    opDialogReference->Show();

    return TransferOwnership(opDialogReference);
}


WeakPtr<IActionCoordinateInputWidget> ActionUIFactory::CreateActionCoordinateInputWidget(const std::wstring& title,
    std::vector<OwnerPtr<ICoordinateInputDefinition>>&& controlDefinitions,
    std::function<bool(const std::wstring &editValue, double &coorDinate, std::wstring &errorTip)> &editCallBack, bool userCustomInput/* = false*/)
{
    DBG_WARN_AND_RETURN_UNLESS(IUiDocumentViewManager::Get(), WeakPtr<IActionCoordinateInputWidget>(), L"IUiDocumentViewManager::Get() 为空",L"GDMPLab",L"2024-03-30");
    IUiView* pUiView = IUiDocumentViewManager::Get()->GetCurrentUiView();
    DBG_WARN_AND_RETURN_UNLESS(pUiView, WeakPtr<IActionCoordinateInputWidget>(), L"pUiView为空",L"GDMPLab",L"2024-03-30");

    //OwnerPtr<ActionCoordinateInputWidget> opWidget(NEW_AS_OWNER_PTR(ActionCoordinateInputWidget, TransferOwnership(controlDefinitions), dynamic_cast<QWidget*>(pUiView)));
    QWidget* pViewWidget = QWidget::find((WId)pUiView->GetUiViewId());
    DBG_WARN_AND_RETURN_UNLESS(pViewWidget, WeakPtr<IActionCoordinateInputWidget>(), L"pViewWidget为空",L"GDMPLab",L"2024-03-30");
    ActionCoordinateInputWidget* pWidget = NEW_AS_QT_CHILD(ActionCoordinateInputWidget, pViewWidget);
    pWidget->InitWidget(TransferOwnership(controlDefinitions), editCallBack, userCustomInput);
    pWidget->SetWindowTitle(title);

    return WeakPtr<IActionCoordinateInputWidget>(pWidget);
}

gcmp::OwnerPtr<gcmp::ICoordinateInputDefinition> ActionUIFactory::CreateCoordinateInputDefinition(const std::wstring& id, const std::wstring& caption, LineEditInputType inputType)
{
    return NEW_AS_OWNER_PTR(CoordinateInputDefinition, id, caption, inputType);
}
